<?php
mysql_select_db($database_drycleaners_conn, $drycleaners_conn);
$query_branch = "SELECT * FROM branch";
$branch = mysql_query($query_branch, $drycleaners_conn) or die(mysql_error());
$row_branch = mysql_fetch_assoc($branch);
$totalRows_branch = mysql_num_rows($branch);

mysql_select_db($database_drycleaners_conn, $drycleaners_conn);
$query_order = "SELECT order_item.id as order_item_id_main,
       order_item.item_id,
       order_item.color,
       order_item.material_id,
       order_item.type_of_service_id,
       order_item.tag,
       order_item.cost,
       order_item.order_id,
       order_item.created_by,
       order_item.created_date as date,
       order_item.deleted_by,
       order_item.delete_date,
       order_item.record_status,
       suborder_item.id,
       suborder_item.suborder_id,
       suborder_item.order_item_id,
       suborder_item.created_by,
       suborder_item.created_date,
       suborder_item.deleted_by,
       suborder_item.delete_date,
       suborder_item.record_status,
       item.name as item_name,
       type_of_service.name as type_of_service_name,
       `order`.order_no,
       material.name as material_name,
         suborder.id,
       suborder.order_id,
       suborder.branch_from_id,
       suborder.branch_to_id,
       suborder.received_date,
       suborder.received_by,
       suborder.received,
       suborder.returned,
       suborder.returned_date,
       suborder.returned_by,
       suborder.receive_returned,
       suborder.receive_returned_date,
       suborder.receive_returned_by,
       suborder.pickup,
       suborder.pickup_date,
       suborder.pickup_issued_by,
       suborder.created_by,
       suborder.created_date as transfered_date	,
       suborder.deleted_by,
       suborder.delete_date,
       suborder.record_status,
       suborder.suborder_no
  FROM    (   (   (   (   (   drycleaners.order_item order_item
                           INNER JOIN
                              drycleaners.type_of_service type_of_service
                           ON (order_item.type_of_service_id =
                                  type_of_service.id))
                       LEFT OUTER JOIN
                          drycleaners.suborder_item suborder_item
                       ON (suborder_item.order_item_id = order_item.id))
                   LEFT OUTER JOIN
                      drycleaners.suborder suborder
                   ON (suborder_item.suborder_id = suborder.id))
               INNER JOIN
                  drycleaners.`order` `order`
               ON (order_item.order_id = `order`.id))
           INNER JOIN
              drycleaners.item item
           ON (order_item.item_id = item.id))
       INNER JOIN
          drycleaners.material material
       ON (order_item.material_id = material.id)
 WHERE  order.created_date BETWEEN '".$_REQUEST['from_date']." 00:00:00"."' AND '".$_REQUEST['to_date']." 23:00:00"."'  AND order.branch_id='".$_REQUEST['branch_id']."'
 GROUP BY order_item.id
 ";
$order = mysql_query($query_order, $drycleaners_conn) or die(mysql_error());
$row_order = mysql_fetch_assoc($order);
$totalRows_order = mysql_num_rows($order);
 get_main_menu($_REQUEST['menu_type']) ?>
   <br/>
   <div style="width:90%; margin-left:auto; margin-right:auto;">
    <div class="form_title">

            <?php echo $_REQUEST["action"];?> Brach Iventory Report Generator <?php echo $row_get_branch_by_id['name']?>
    </div>
    <div class="box"> 
<form action="<?php echo $editFormAction; ?>" method="post" name="form1" id="form1">
  <table align="left">
         <tr valign="middle">
      <td height="32" align="left" nowrap="nowrap">From Date</td>
      <td align="center"><script>DateInput('from_date', true, 'YYYY-MON-DD')</script></td>

      <td align="left" nowrap="nowrap">To Date</td>
      <td><script>DateInput('to_date', true, 'YYYY-MON-DD')</script></td>
 	<?php if(!isset($_SESSION['branch_id'])){?>
      <td align="left" nowrap="nowrap">&nbsp;</td>
      <td>
     
		<select name=" branch_id">
		  <?php do {  ?>
		  <option value="<?php echo $row_branch['id']?>"><?php echo $row_branch['name']?></option>
		  <?php  } while ($row_branch = mysql_fetch_assoc($branch));
		  $rows = mysql_num_rows($branch);
		  if($rows > 0) {
			  mysql_data_seek($branch, 0);
			  $row_branch = mysql_fetch_assoc($branch);
			   }
			   ?>
		</select>
	</td>
    <?php }else{?>
		<td><input hidden="" name="branch_id" value="<?php echo $_SESSION['branch_id']?>" /></td>
	<?php }?>
      <td align="left" nowrap="nowrap">&nbsp;</td>
      <td valign="top">
      
		  <input name='create_report' type='submit' value='Search' id='submit' style="width:100px; height:30px; margin-top:0px; padding-top:5px;"/>
          <?php if(isset($_REQUEST['create_report'])){?>
          <a href="?branch_report_exporter&branch_id=<?php echo $_REQUEST['branch_id'] ?>&from_date=<?php echo $_REQUEST['from_date'] ?>&to_date=<?php echo $_REQUEST['to_date'] ?>" target="_blank"><input name='create_report' type='button' value='Export' id='submit' style="width:100px; height:30px; margin-top:0px;padding-top:5px;"/></a>
          <?php }?>
		   </td>
    </tr>
  </table>
  <input type="hidden" name="record_status" value="active" />
  <input type="hidden" name="MM_insert" value="report_form" />
</form>

</div>

<div>

<?php
if(isset($_REQUEST['create_report'])){
	//header( "Content-Type: application/vnd.ms-excel" );
	//header( "Content-disposition: attachment; filename=spreadsheet.xls" );

?>
<?php include"includes/table_settings.php"?>
<table  cellpadding="0" cellspacing="0" border="0" id="dataTable">
   <thead>
    <tr>
        <th>Item Id</th>
        <th>Item Detail</th>
        <th>Order No</th>
        <th>Date</th>
        <th>Transfered</th>
        <th>Received By Branch</th>
        <th>Returned By Branch</th>
       <th title="Received By Receprion From Branch">Received By Reception</th>
        <th>Picked Up</th>
    </tr>
    </thead>
<?php
$branch_total_bill=0;
$branch_total_payment=0;
$branch_total_balance=0;
if($totalRows_order>0){
do{
?>
<tr>
	<td align="center"><?php echo $row_order["order_item_id_main"]?></td>
    <td align="center"><?php echo $row_order["color"]." ".$row_order["material_name"]." ".$row_order["item_name"]." On ".$row_order["type_of_service_name"]?></td>
    <td align="center"><?php echo $row_order["order_no"]?></td>
    <td align="center">
    <?php echo date("d-M-Y H:i:s",strtotime($row_order["date"]));?>
	</td>
    <td align="center">
    <?php 
	if($row_order["transfered_date"]){
	echo "Transfered";
	}
	else{
		echo "Untransfered";
		}
	?>
    </td>
    <td align="center"><?php echo $row_order['received'];?></td>
    <td align="center"><?php echo $row_order['returned']; ?></td>
    <td align="center"><?php echo $row_order["receive_returned"]?></td>
    <td align="center"><?php echo $row_order["pickup"]?></td>
</tr>
<?php
 }while($row_order=mysql_fetch_assoc($order));
}
?>
    <tr>
        <th></th>
        <th></th>
        <th></th>
        <th><?php //echo $branch_total_bill;?></th>
        <th><?php //echo $branch_total_payment;?></th>
        <th></th>
        <th></th>
        <th></th>
        <th></th>
    </tr>
</table>
<?php
}
?>
</div>
    <?php
mysql_free_result($branch);

mysql_free_result($order);
?>
